import {createRouter, createWebHistory} from 'vue-router'
import jsCookie from "jscookie";
import {checkAdmin, checkDoctor, checkPatient} from "@/api/login.js";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/login/login.vue')
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('../views/register/register.vue')
        },
        {
            path: '/patientHome',
            name: 'patientHome',
            component: () => import('../views/patient/patientHome.vue'),
            children: [
                {
                    path: '',
                    name: 'userCenter',
                    component: () => import('../views/patient/userCenter/userCenter.vue')
                },
                {
                    path: 'reg',
                    name: 'reg',
                    component: () => import('@/views/patient/reg/reg.vue')
                },
                {
                    path: 'op',
                    name: 'op',
                    component: () => import('@/views/patient/op/op.vue')
                },
                {
                    path: 'mr',
                    name: 'mr',
                    component: () => import('@/views/patient/mr/mr.vue')
                },
            ]
        },
        {
            path: '/doctorHome',
            name: 'doctorHome',
            component: () => import('../views/doctor/doctorHome.vue'),
            children: [
                {
                    path: 'callNum',
                    name: 'callNum',
                    component: () => import('@/views/doctor/callNum/callNum.vue')
                },
                {
                    path: 'drug',
                    name: 'drug',
                    component: () => import('@/views/doctor/drug/drug.vue')
                },
                {
                    path: 'inv',
                    name: 'inv',
                    component: () => import('@/views/doctor/inv/inv.vue')
                },
            ]
        },
        {
            path: '/adminHome',
            name: 'adminHome',
            component: () => import('../views/admin/adminHome.vue'),
            children: [
                {
                    path: '',
                    name: 'home',
                    component: () => import('@/views/admin/home/home.vue')
                },
                {
                    path: 'docManage',
                    name: 'docManage',
                    component: () => import('@/views/admin/docManage/docManage.vue')
                },
                {
                    path: 'deptManage',
                    name: 'deptManage',
                    component: () => import('@/views/admin/deptManage/deptManage.vue')
                },
                {
                    path: 'aiManage',
                    name: 'aiManage',
                    component: () => import('@/views/admin/aiManage/aiManage.vue')
                },
                {
                    path: 'outbound',
                    name: 'outbound',
                    component: () => import('@/views/admin/outbound/outbound.vue')
                },
                {
                    path: 'inbound',
                    name: 'inbound',
                    component: () => import('@/views/admin/inbound/inbound.vue')
                },
            ]
        },
    ]
})

router.beforeEach(async (to, from) => {
    const token = jsCookie.get('token');
    const type = jsCookie.get('type');
    if (!token && !type && to.name !== 'login' && to.name !== 'register') {
        // 将用户重定向到登录页面
        return {name: 'login'}
    } else if (to.name !== 'login' && to.name !== 'register') {
        let isLogin = false;
        if (type == "pat") {
            await checkPatient(token).then((response) => {
                if (response.data.code == 200) {
                    isLogin = true;
                }
            })
        } else if (type == "doc") {
            await checkDoctor(token).then((response) => {
                if (response.data.code == 200) {
                    isLogin = true;
                }
            })
        } else {
            await checkAdmin(token).then((response) => {
                if (response.data.code == 200) {
                    isLogin = true;
                }
            })
        }
        if (!isLogin) {
            // 将用户重定向到登录页面
            return {name: 'login'}
        }
    }
})


export default router
